package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MultiPointItem;
import com.amap.api.maps.model.MultiPointOverlay;
import com.amap.api.maps.model.MultiPointOverlayOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class PoiAroundSearchActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    private PoiSearch poiSearch;

    private EditText etKey;

    private MultiPointOverlay multiPointOverlay;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi_around_search);

        etKey = (EditText)findViewById(R.id.et_key);
        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    public void searchByKey(View view) {
        String key = etKey.getText().toString();
        if (key.equalsIgnoreCase("")) {
            Toast.makeText(this, "关键字不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        PoiSearch.Query query = new PoiSearch.Query(key, "", "");
        query.setPageSize(10);
        query.setPageNum(0);

        poiSearch = new PoiSearch(this, query);
        LatLonPoint latLon = new LatLonPoint(39.993743, 116.472995);
        poiSearch.setBound(new PoiSearch.SearchBound(latLon, 5000, true));
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int rCode) {
                Log.e("TAG", "onPoiSearched");
                ArrayList<PoiItem> pois = poiResult.getPois();
                if (pois == null) {
                    return;
                }
                List<MultiPointItem> list = new ArrayList<>();
                for (PoiItem item : pois) {
                    LatLonPoint latLonPoint = item.getLatLonPoint();
                    list.add(new MultiPointItem(new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude())));
                }
                showResultOnMap(list);
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int rCode) {
                Log.e("TAG", "onPoiItemSearched");
            }
        });
        poiSearch.searchPOIAsyn();
    }

    private void showResultOnMap(List<MultiPointItem> list) {
        MultiPointOverlayOptions overlayOptions = new MultiPointOverlayOptions();
        overlayOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_mdl));
        overlayOptions.anchor(0.5f,0.5f);

        multiPointOverlay = aMap.addMultiPointOverlay(overlayOptions);
        multiPointOverlay.setItems(list);

        AMap.OnMultiPointClickListener multiPointClickListener = new AMap.OnMultiPointClickListener() {
            @Override
            public boolean onPointClick(MultiPointItem pointItem) {
                Log.e("TAG", "pointItem=" + pointItem.getLatLng());
                Toast.makeText(PoiAroundSearchActivity.this, "pointItem=" + pointItem.getLatLng(), Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        aMap.setOnMultiPointClickListener(multiPointClickListener);
    }

    public void removeAll(View view) {
        multiPointOverlay.remove();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
